<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-02-24
 * Time: 19:42
 */
namespace app\student\controller;

use app\student\model\Yy as YyModel;
use app\admin\controller\Base;
use think\Config;
class Yy extends Base
{
    public function index(){
        $datemin = $this->request->param('start')?strtotime($this->request->param('start').' 24:00:00'):'';
        $datemax = $this->request->param('end')?strtotime($this->request->param('end').' 24:00:00'):time();

        // 获取数据
        $list = YyModel::where('name|nickName|phone','like','%'.$this->request->param('keywords').'%')
            ->where('opttime','between',[$datemin,$datemax])
            ->where('status',0)
            ->paginate(Config::get('paginate')['list_rows']);

        $this->assign('list',$list);

        //print_r($list);

        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }

    public function adddo(){
        $data = array(
            'name' => $this->request->param('name'),
            'sex' => $this->request->param('sex'),
            'age' => $this->request->param('age'),
            'phone' => $this->request->param('phone'),
            'nickName' => $this->request->param('nickName'),
            'opttime' => time(),
            'uname' => $this->request->session('kj.uname'),
            'yyitem' => $this->request->param('yyitem'),
            'desc' => $this->request->param('desc'),
        );
        $ids = explode(',',$this->request->param('ids'));
        if(count($data)>=8){
            $check = YyModel::get(['nickName'=>$data['nickName']]);
            if($check){
                $res = ['statusCode'=>300,'message'=>'昵称账号已存在'];
            }else{
                if($insert = YyModel::create($data)){
                    $res = ['statusCode'=>200,'message'=>'操作成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function edit(){
        $this->assign('info',YyModel::get($this->request->param('id')));
        return $this->fetch();
    }

    public function editdo(){
        $data = array(
            'id' => $this->request->param('id'),
            'name' => $this->request->param('name'),
            'sex' => $this->request->param('sex'),
            'age' => $this->request->param('age'),
            'phone' => $this->request->param('phone'),
            'nickName' => $this->request->param('nickName'),
            'opttime' => time(),
            'uname' => $this->request->session('kj.uname'),
            'yyitem' => $this->request->param('yyitem'),
            'desc' => $this->request->param('desc'),
        );
        $ids = explode(',',$this->request->param('ids'));
        if(count($data)>=9){
            $check = YyModel::where('nickName',$data['nickName'])->where('id','<>',$data['id'])->find();
            if($check){
                $res = ['statusCode'=>300,'message'=>'昵称账号已存在'];
            }else{
                if($update= YyModel::update($data)){
                    $res = ['statusCode'=>200,'message'=>'操作成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function delete(){
        $ids = explode(',',$this->request->param('ids'));
        $i = 0;
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                YyModel::destroy($value);
                $i++;
            }
            if($i>0){
                $res = ['statusCode'=>200,'message'=>'操作完成'];
            }else{
                $res = ['statusCode'=>300,'message'=>'请先禁用操作项后在执行此操作'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    public function set(){
        $status = $this->request->param('status');
        $id = $this->request->param('id');
        if($id){
            YyModel::where('id',$id)->update(['status'=>$status]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }


}